﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BBMS.DLL.Forms.Base;
using BBMS.DLL.DataContract;
using BBMS.DLL.Base;
using ExcelLibrary.SpreadSheet;
namespace BBMS.DLL.Forms.Bill.Discount
{
    public partial class frmDiscountFilter : FrmFilterBase
    {
        public frmDiscountFilter()
        {
            InitializeComponent();
            GetMaNCCList();
        }

        private void GetMaNCCList()
        {
            ArrayList arret = NhaCungCapList.getAllMaNCC();
            cbxMaNCC.Items.Add(new KeyValuePair("", "Tất cả"));
            if (arret != null)
            {
                for (int i = 0; i < arret.Count; i++)
                {
                    KeyValuePair kvp = (KeyValuePair)arret[i];
                    cbxMaNCC.Items.Add(kvp);
                }
            }
            cbxMaNCC.SelectedIndex = 0;
        }

        public new static readonly string FunctionKey = "CT_CTCK";
        protected override string GetFunctionKey() { return "CT_CTCK"; }
        protected override BBMS.DLL.Base.ObjectBase GetObjectBase() { return new ChungTuChietKhau(); }

        protected override void GenColHeaders()
        {
            lvList.Columns.Clear();
            //lvList.Columns.Add("Ký hiệu", 80);
            //lvList.Columns.Add("Số hiệu", 80);
            lvList.Columns.Add("Ngày CT", 80);
            lvList.Columns[0].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Nhà cung cấp", 200);
            lvList.Columns.Add("Mã Quầy", 80);
            lvList.Columns[2].TextAlign = HorizontalAlignment.Right;
            lvList.Columns.Add("Quầy", 150);
            lvList.Columns.Add("Giá trị", 100);
            lvList.Columns[4].TextAlign = HorizontalAlignment.Right;
        }
        protected override ArrayList GenSampleData()
        {          
            ArrayList ar = new ArrayList();
            ar.Add(new string[] { "STT", "Ngày chứng từ", "Hạn thanh toán", "Ngày thanh toán dự kiến", "Ngày thanh toán"
            ,"Trạng thái thanh toán", "Mã hồ sơ", "Mã nhà cung cấp", "Mã quầy", "Giá trị chứng từ"
            ,"Giá trị đã thanh toán", "Ghi chú", "Mã nhân viên"});
            ChungTuChietKhau chungTuChietKhauList = new ChungTuChietKhau();
            ArrayList arr = chungTuChietKhauList.GetAllObject(fParams);
            if (arr != null)
            {
                for (int i = 0; i < arr.Count; i++)
                {
                    ChungTuChietKhau ctck = (ChungTuChietKhau)arr[i];
                    ar.Add(new string[] { ctck.ID.ToString(), ctck.NgayCT.ToString(), ctck.NgayHT.ToString(), ctck.NgayTTDuKien.ToString()
                    ,ctck.NgayTT.ToString(),((int)ctck.TrangThaiTT).ToString(),ctck.MaHoSo,ctck.MaNCC
                    ,ctck.MaQuay,ctck.GiaTriCT.ToString(),ctck.GiaTriDaTT.ToString(), ctck.GhiChu, ctck.NVID.ToString()});
                }
            }
            return ar;
        }

        protected override ArrayList GetImportedList(Worksheet sheet)
        {
            ArrayList arret = new ArrayList();
            for (int i = 1; i < sheet.Cells.Rows.Count; i++)
            {
                ChungTuChietKhau ctck = new ChungTuChietKhau();
                Cell cell = sheet.Cells[i, 0];
                ctck.ID = Convert.ToInt32(cell.Value.ToString());              
                cell = sheet.Cells[i, 1];
                if (cell.Value.ToString() != "") ctck.NgayCT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 2];
                if (cell.Value.ToString() != "") ctck.NgayHT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 3];
                if (cell.Value.ToString() != "") ctck.NgayTTDuKien = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 4];
                if (cell.Value.ToString() != "") ctck.NgayTT = Convert.ToDateTime(cell.Value.ToString());
                cell = sheet.Cells[i, 5];
                foreach (TrangThaiThanhToan item in Enum.GetValues(typeof(TrangThaiThanhToan)))
                {
                    if (item.ToString() == cell.Value.ToString())
                        ctck.TrangThaiTT = item;
                }
                cell = sheet.Cells[i, 6];
                ctck.MaHoSo = cell.Value.ToString();
                cell = sheet.Cells[i, 7];
                ctck.MaNCC = cell.Value.ToString();
                cell = sheet.Cells[i, 8];
                ctck.MaQuay = cell.Value.ToString();                
                cell = sheet.Cells[i, 9];
                if (cell.Value.ToString() != "") ctck.GiaTriCT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 10];
                if (cell.Value.ToString() != "") ctck.GiaTriDaTT = Convert.ToInt64(cell.Value.ToString());
                cell = sheet.Cells[i, 11];
                ctck.GhiChu = cell.Value.ToString();
                cell = sheet.Cells[i, 12];
                if (cell.Value.ToString() != "") ctck.NVID = Convert.ToInt32(cell.Value.ToString());
              
                arret.Add(ctck);

            }
            return arret;
        }

        protected override void UpdateFilterParams()
        {
            if (txtMa.Text.Trim() != "" || txtSoCT.Text.Trim() != "" || cbxMaNCC.SelectedIndex != -1 
                || txtGiaTriCTTu.Text.Trim() != "0" || txtGiaTriCTDen.Text.Trim() != "0")
            {
                fParams = new FilterParams();
                //FilterCondition fcLoaiCTCK = new FilterCondition("LoaiCT", "LoaiCT", "CTCK", SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcLoaiCTCK);
                //FilterCondition fcIsChi = new FilterCondition("IsChi", "IsChi", false, SQLOperator.EQUAL);
                //fParams.ArParams.Add(fcIsChi);
                if (txtMa.Text.Trim() != "")
                {
                    FilterCondition fcMa = new FilterCondition("MaCT", "MaCT", "%" + txtMa.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcMa);
                }

                if (txtSoCT.Text.Trim() != "")
                {
                    FilterCondition fcSoCT = new FilterCondition("SoCT", "SoCT", "%" + txtSoCT.Text.Trim() + "%", SQLOperator.LIKE);
                    fParams.ArParams.Add(fcSoCT);
                }

                if (cbxMaNCC.SelectedIndex != 0)
                {
                    FilterCondition fcMaNCC = new FilterCondition("MaNCC", "MaNCC", ((KeyValuePair)cbxMaNCC.SelectedItem).Key, SQLOperator.EQUAL);
                    fParams.ArParams.Add(fcMaNCC);
                }

                if (txtGiaTriCTTu.Text.Trim() != "" && txtGiaTriCTTu.Text.Trim()!="0")
                {
                    FilterCondition fcGiaTriCTTu = new FilterCondition("GiaTriCT", "GiaTriCT", txtGiaTriCTTu.Text.Trim().Replace(",",""), SQLOperator.GREATER_OR_EQUAL);
                    fParams.ArParams.Add(fcGiaTriCTTu);
                }

                if (txtGiaTriCTDen.Text.Trim() != "" && txtGiaTriCTDen.Text.Trim() != "0")
                {
                    FilterCondition fcGiaTriCTDen = new FilterCondition("GiaTriCT", "GiaTriCT", txtGiaTriCTDen.Text.Trim().Replace(",", ""), SQLOperator.LOWER_OR_EQUAL);
                    fParams.ArParams.Add(fcGiaTriCTDen);
                }

              
                fParams.CurrentPage = 0;
            }
            else
            {
                MessageBox.Show(null, "Bạn phải nhập nội dung tìm kiếm", "Thông báo");
            }
        }

        protected override FrmEditBase CreateEditForm(BBMS.DLL.Base.ObjectBase obj)
        {
            return new frmDiscountUpdate(obj);
        }

        protected override void AddNewObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            if (obj == null) return;
            //ListViewItem it = new ListViewItem(((ChungTuChietKhau)obj).MaCT);
            //it.SubItems.Add(((ChungTuChietKhau)obj).SoCT);
            ListViewItem it = new ListViewItem(((ChungTuChietKhau)obj).NgayCT.ToString("dd/MM/yyyy"));
            //it.SubItems.Add(((ChungTuChietKhau)obj).NgayCT.ToString("dd/MM/yyyy"));
            it.SubItems.Add(ObjectLists.NhaCungCapList.getObjectName(((ChungTuChietKhau)obj).MaNCC));
            it.SubItems.Add(((ChungTuChietKhau)obj).MaQuay);
            it.SubItems.Add(ObjectLists.QuayList.getObjectName(((ChungTuChietKhau)obj).MaQuay));
            it.SubItems.Add(CommonTools.Helper.MoneyString(((ChungTuChietKhau)obj).GiaTriCT));
            it.Tag = obj;
            lvList.Items.Add(it);
        }

        protected override void UpdateObjectToList(BBMS.DLL.Base.ObjectBase obj)
        {
            foreach (ListViewItem it in lvList.Items)
            {
                if (((ChungTuChietKhau)it.Tag).MaCT == ((ChungTuChietKhau)obj).MaCT)
                {
                    it.SubItems[0].Text = ((ChungTuChietKhau)obj).NgayCT.ToString("dd/MM/yyyy");
                    it.SubItems[1].Text = ObjectLists.NhaCungCapList.getObjectName(((ChungTuChietKhau)obj).MaNCC);
                    it.SubItems[2].Text = ((ChungTuChietKhau)obj).MaQuay;
                    it.SubItems[3].Text = ObjectLists.QuayList.getObjectName(((ChungTuChietKhau)obj).MaQuay);
                    it.SubItems[4].Text = CommonTools.Helper.MoneyString(((ChungTuChietKhau)obj).GiaTriCT);
                    it.Tag = obj;
                    break;
                }
            }
        }

        //protected override void OnRefreshClick()
        //{
        //    fParams = new FilterParams();
        //    FilterCondition fcLoaiCTCK = new FilterCondition("LoaiCT", "LoaiCT", "CTCK", SQLOperator.EQUAL);
        //    fParams.ArParams.Add(fcLoaiCTCK);
        //    FilterCondition fcIsChi = new FilterCondition("IsChi", "IsChi", false, SQLOperator.EQUAL);
        //    fParams.ArParams.Add(fcIsChi);
        //    ListObject();
        //}    
    }
}
